<?php
class projectAction extends BaseAction
{
    public function index(){

    }
	public function add()
	{
        $this->require_login();
        $form_data = session('form_data');
        if(isset($form_data['province']) && empty($form_data['province'])){
            $form_data['province'] = '0';
        }
        if(isset($form_data['city']) && empty($form_data['city'])){
            $form_data['city'] = '0';
        }
        if(isset($form_data['description'])){
            $form_data['description'] = stripslashes($form_data['description']);
            $form_data['description'] = str_replace('./','/',$form_data['description']);
        }
        $form_data['limit_price'] = sprintf('%.2f',$form_data['limit_price'] ? floatval($form_data['limit_price']) : '0.00');
		$this->assign("page_title","发起项目");
		$cate_list = M('deal_cate')->order('sort asc')->select();
		$this->assign("cate_list",$cate_list);
		
		$deal_image =  session("deal_image");
		$this->assign("deal_image",$deal_image);
        $this->assign('deal_item',$form_data);
		$this->display();
	}
	
	public function edit()
	{
        $this->require_login();
		$id = intval($_REQUEST['id']);
		$deal_item = M('deal')->where(array('id'=>$id,'is_effect'=>array('IN',array('0','2')),'is_delete'=>'0','user_id'=>intval($this->uid)))->find();
		if($deal_item)
		{
            $deal_item['description'] = stripslashes($deal_item['description']);
            $deal_item['description'] = str_replace('./','/',$deal_item['description']);
            $form_data = session('form_data');
            if(isset($form_data['description'])){
                $form_data['description'] = stripslashes($form_data['description']);
                $form_data['description'] = str_replace('./','/',$form_data['description']);
            }
//            print_r($form_data);
            if($deal_item['id'] == $form_data['id']){
                $deal_item = array_replace($deal_item,$form_data);
            }
            else{
                session('form_data',null);
            }
            if(isset($deal_item['province']) && empty($deal_item['province'])){
                $deal_item['province'] = '0';
            }
            if(isset($deal_item['city']) && empty($deal_item['city'])){
                $deal_item['city'] = '0';
            }
            $deal_item['limit_price'] = sprintf('%.2f',$deal_item['limit_price'] ? floatval($deal_item['limit_price']) : '0.00');
			$this->assign("page_title",$deal_item['name']);
			$deal_item['faq_list'] = M('deal_faq')->where(array('deal_id'=>$deal_item['id']))->order('sort asc')->select();
			$cate_list = M('deal_cate')->order('sort asc')->select();
			$this->assign("cate_list",$cate_list);
			$this->assign("deal_item",$deal_item);
			
			$this->display();
		}	
		else
		{
			$this->error("编辑的项目不存在");
		}		
		
	}
	
	
	public function save()
	{
		session('form_data',$_REQUEST);
		$ajax = intval($_REQUEST['ajax']);
        $this->require_login();
		$id =  intval($_REQUEST['id']);
		
		$data['name'] = strim($_REQUEST['name']);
		if($data['name']=="")
		{
            $this->error('请填写项目名称');
		}
		if(msubstr($data['name'],0,40)!=$data['name'])
		{			
			$this->error('项目名称不超过40个字');
		}
		$data['cate_id'] = intval($_REQUEST['cate_id']);
		if($data['cate_id']==0)
		{
			$this->error('请选择项目分类');
		}
		$data['province'] = strim($_REQUEST['province']);
		if($data['province']=='')
		{
			$this->error('请选择省份');
		}
		$data['city'] = strim($_REQUEST['city']);
		if($data['city']=='')
		{
			$this->error('请选择城市');
		}
		$data['brief'] = strim($_REQUEST['brief']);
        if(msubstr($data['brief'],0,75) != $data['brief']){
            $this->error('项目简介不能超过75个字');
        }
		$data['image'] = addslashes(trim($_REQUEST['image']));
		if($data['image']=="")
		{			
			$this->error('请上传封面图片');
		}
		
		require_once SITE_PATH . "system/libs/words.php";
		$data['tags'] = implode(" ",words::segment($data['name']));

        //todo 如果加上valid_tag方法，文本编辑器的粗体、斜体、删除线、分割线将无法使用
		$data['description'] = addslashes(trim($_REQUEST['description']));

		$data['video'] = strim($_REQUEST['video']);
		
		if($data['video']!="")
		{
			require_once SITE_PATH . "system/utils/video.php";
			$video = fetch_video_url($data['video']);
			if($video!="")
			{
				$data['source_video'] =  $video;
			}
			else
			{
				$this->error('非法的视频地址');
			}
		}
		
		$data['limit_price'] = doubleval($_REQUEST['limit_price']);
		if($data['limit_price']<=0)
		{
			$this->error('请输入正确的目标金额');
		}
		$data['deal_days'] = intval($_REQUEST['deal_days']);
		if($data['deal_days']<=0)
		{
			$this->error('请输入正确的筹集天数');
		}
		
		if($id>0)
		{
			$savenext = intval($_REQUEST['savenext']);
            $deal_item = M('deal')->where(array('id'=>$id,'is_delete'=>'0','user_id'=>intval($this->uid)))->find();
            if(!$deal_item){
                $this->error('找不到要保存的项目');
            }
            if($deal_item['is_effect'] == '1'){
                $this->error('项目已经上线，不能修改');
            }
            M('deal')->where(array('id'=>$id))->data($data)->save();
			//追加faq
			M('deal_faq')->where(array('deal_id'=>$id))->delete();
			$sort = 1;
			foreach($_REQUEST['question'] as $kk=>$question_item)
			{
				if(strim($_REQUEST['question'][$kk])!=""&&strim($_REQUEST['answer'][$kk])!=""&&strim($_REQUEST['question'][$kk])!="请输入问题"&&strim($_REQUEST['answer'][$kk])!="请输入答案")
				{
					$faq_item['deal_id'] = $id;
					$faq_item['question'] = strim($_REQUEST['question'][$kk]);
					$faq_item['answer'] = strim($_REQUEST['answer'][$kk]);
					$faq_item['sort'] = $sort;
					M('deal_faq')->data($faq_item)->add();
					$sort++;
				}
			}
			
			if($savenext==0)
			{
                session('form_data' , null);
                $this->success('添加成功');
			}
			else
			{
                session('form_data' , null);
                $this->redirect('/project/add_item',array("deal_id"=>$id),0,'');
			}
		}
		else
		{
			$data['user_id'] = intval($this->uid);
			$data['user_name'] = $this->username;
			$data['create_time'] = TIME_NOW;
            $data['is_effect'] = '2';
			$savenext = intval($_REQUEST['savenext']);
			$data_id = M('deal')->data($data)->add();
			if($data_id==0)
			{
                $this->error('保存失败，请联系管理员');
			}
			else
			{
				session("deal_image",null);
				
				//追加faq
				$sort = 1;
				foreach($_REQUEST['question'] as $kk=>$question_item)
				{
					if(strim($_REQUEST['question'][$kk])!=""&&strim($_REQUEST['answer'][$kk])!=""&&strim($_REQUEST['question'][$kk])!="请输入问题"&&strim($_REQUEST['answer'][$kk])!="请输入答案")
					{
						$faq_item['deal_id'] = $data_id;
						$faq_item['question'] = strim($_REQUEST['question'][$kk]);
						$faq_item['answer'] = strim($_REQUEST['answer'][$kk]);
						$faq_item['sort'] = $sort;
						M('deal_faq')->data($faq_item)->add();
						$sort++;
					}
				}
				if($savenext==0)
				{
                    session('form_data' , null);
                    $this->success('添加成功');
				}
				else
				{
                    session('form_data' , null);
                    $this->redirect('/project/add_item',array("deal_id"=>$data_id),0,'');
				}
			}
			
		}		
		
	}
	
	public function del()
	{
        $this->require_login();
		$id = intval($_REQUEST['id']);
		
		$res = M('deal')->where(array('id'=>$id,'user_id'=>intval($this->uid),'is_effect'=>array('IN',array('0','2')),'is_delete'=>'0'))->delete();
		if($res>0)
		{
			M('deal_item')->where(array('deal_id'=>$id))->delete();
			M('deal_item_image')->where(array('deal_id'=>$id))->delete();
			M('deal_comment')->where(array('deal_id'=>$id))->delete();
			M('deal_faq')->where(array('deal_id'=>$id))->delete();
			M('deal_focus_log')->where(array('deal_id'=>$id))->delete();
			M('deal_log')->where(array('deal_id'=>$id))->delete();
			M('deal_pay_log')->where(array('deal_id'=>$id))->delete();
			M('deal_support_log')->where(array('deal_id'=>$id))->delete();
			M('deal_visit_log')->where(array('deal_id'=>$id))->delete();
			$this->success('删除项目成功');
		}
		else
		{
			$this->error('删除失败,项目不存在或者项目已经上线');
		}
		
	
	}
	
	public function add_item()
	{
        $this->require_login();
        session('form_data' , null);
		$id = intval($_REQUEST['deal_id']);
		$deal_item = M('deal')->where(array('is_effect'=>array('IN',array('0','2')),'is_delete'=>'0','id'=>$id,'user_id'=>intval($this->uid)))->find();
		if($deal_item)
		{		

			$deal_item_list = M('deal_item')->where(array('deal_id'=>$deal_item['id']))->order('price asc')->select();
			$this->assign("deal_item_list",$deal_item_list);
			
			$this->assign("deal_item",$deal_item);
			$this->assign("page_title","回报设置 - ".$deal_item['name']);
            $this->display();
		}else{
            $this->error('找不到该项目或者该项目不是您发布的,请核实');
        }

	}
	
	public function edit_item()
	{
        $this->require_login();
		$id = intval($_REQUEST['id']);
		$item = M('deal_item')->where(array('id'=>$id))->find();
		$deal_item = M('deal')->where(array('is_effect'=>array('IN',array('0','2')),'is_delete'=>'0','id'=>$item['deal_id'],'user_id'=>intval($this->uid)))->find();
		if($deal_item&&$item)
		{		
			$deal_item_images = M('deal_item_image')->where(array('deal_id'=>$deal_item['id'],'deal_item_id'=>$item['id']))->select();
            $item['image'] = $deal_item_images;
            $item['price'] = sprintf('%.2f',$item['price']);
            $this->return_status('1',$item);
		}else{
            $this->return_status('0','找不到要编辑的项目');
        }
	}
	
	public function del_item()
	{
        $this->require_login();
		$id = intval($_REQUEST['id']);
		$item = M('deal_item')->where(array('id'=>$id))->find();
		$deal_item = M('deal')->where(array('is_effect'=>'0','is_delete'=>'0','id'=>$item['deal_id'],'user_id'=>intval($this->uid)))->find();
		if($deal_item&&$item)
		{		
			M('deal_item')->where(array('id'=>$id))->delete();
			M('deal_item_image')->where(array('deal_item_id'=>$id))->delete();
            $this->return_status('1','删除项目产品项成功');
			
		}
		else
		{
            $this->return_status('0','删除项目产品项失败');
		}
	}
	
	public function save_deal_item()
	{
        $this->require_login();
		$id = intval($_REQUEST['id']);
        $row = M('deal')->where(array('is_effect'=>array('IN',array('0','2')),'is_delete'=>'0','id'=>$_REQUEST['deal_id'],'user_id'=>intval($this->uid)))->find();
		if(!isset($row) || $row['user_id'] != $this->uid){
            $this->return_status('0','找不到项目信息');
        }
        $data['price'] = doubleval($_REQUEST['price']);
		if($data['price']<=0){
            $this->return_status('0','请输入正确的价格');
        }
        $data['price'] = sprintf('%.2f',$data['price']);
        $data['title'] = strim($_REQUEST['title']);
		$data['description'] = strim($_REQUEST['description']);
		$data['is_delivery'] = intval($_REQUEST['is_delivery']);
		$data['delivery_fee'] = doubleval($_REQUEST['delivery_fee']);
        if($data['is_delivery'] == 1 && $data['delivery_fee'] < 0){
            $this->return_status('0','运费输入错误');
        }
		$data['is_limit_user'] = intval($_REQUEST['is_limit_user']);
		$data['limit_user'] = intval($_REQUEST['limit_user']);
		$data['repaid_day'] = intval($_REQUEST['repaid_day']);
		$data['deal_id'] = intval($_REQUEST['deal_id']);
        $data['repaid_type'] = intval($_REQUEST['repaid_type']) == 0 ? '0' : '1';
        $data['voucher'] = intval($_REQUEST['voucher']) == 0 ? '0' : '1';

		if(count($_REQUEST['image'])>4)
		{
            $this->return_status('0','图片不能超过四张');
		}
		
		if($id==0)
		{
			$result_id = M('deal_item')->data($data)->add();
			if($result_id>0)
			{
				if(count($_REQUEST['image'])>=0)
				{
					foreach($_REQUEST['image'] as $k=>$v)
					{
                        if(!empty($v)) {
                            $image_data['deal_id'] = $data['deal_id'];
                            $image_data['deal_item_id'] = $result_id;
                            $image_data['image'] = $v;
                            M('deal_item_image')->data($image_data)->add();
                        }
					}
				}
                $this->return_status($result_id,'保存成功');
			}
			else
			{
                $this->return_status('0','保存失败');
			}
		}
		else
		{
			M('deal_item')->where(array('id'=>$id))->data($data)->save();
			if(count($_REQUEST['image'])>=0)
			{
					M('deal_item_image')->where(array('deal_item_id'=>$id))->delete();
					foreach($_REQUEST['image'] as $k=>$v)
					{
                        if(!empty($v)){
                            $image_data['deal_id'] = $data['deal_id'];
                            $image_data['deal_item_id'] = $id;
                            $image_data['image'] = strim($v);
                            M('deal_item_image')->data($image_data)->add();
                        }

					}
			}
            $this->return_status($id,'保存成功');
		}
		
	}


    public function information(){
        if($this->isPost()){
            $data = array();
            $deal_id = intval($_REQUEST['deal_id']);
            $row = M('deal')->where(array('is_effect'=>array('IN',array('0','2')),'is_delete'=>'0','id'=>$deal_id,'user_id'=>intval($this->uid)))->find();
            if(!isset($row) || $row['user_id'] != $this->uid){
                $this->return_status('0','找不到项目信息');
            }
            $data['intro'] = strim($_REQUEST['intro']);//自我介绍
            if(msubstr($data['intro'],0,500) != $data['intro']){
                $this->return_status('0','自我介绍不能超过500个字');
            }
            if(!intval($_REQUEST['pro_agreement_doc'])){
                $this->return_status('0','您的项目提交审核前，需仔细阅读并同意项目发起人协议');
            }

            $data['province'] = strim($_REQUEST['province']);//省份
            $data['city'] = strim($_REQUEST['city']);//城市

            //保存银行卡信息
            $data['ex_bank_name']=addslashes(trim($_REQUEST['ex_bank_name']));
            $data['ex_bank_branch']=addslashes(trim($_REQUEST['ex_bank_branch']));
            $data['ex_bank_author']=addslashes(trim($_REQUEST['ex_bank_author']));
            $data['ex_account_info']=addslashes(trim($_REQUEST['ex_account_info']));
            if($this->user['ex_real_name'] !=''){
                $data['ex_bank_author'] = $this->user['ex_real_name'];
            }else{
                $data['ex_bank_author'] = $data['ex_real_name'] = addslashes(trim($_REQUEST['ex_real_name']));
                $this->check_empty($data['ex_real_name'],'真实姓名不能为空');
            }

            $this->check_empty($data['province'],'所在地省份不能为空');
            $this->check_empty($data['city'],'所在地城市不能为空');
            $this->check_empty($data['intro'],'自我介绍不能为空');
            $this->check_empty($data['ex_bank_name'],'银行名称不能为空');
            $this->check_empty($data['ex_bank_branch'],'开户支行不能为空');
            $this->check_empty($data['ex_bank_author'],'开户名称不能为空');
            $this->check_empty($data['ex_account_info'],'银行账号不能为空');

            M('deal')->where(array('is_effect'=>array('IN',array('0','2')),'is_delete'=>'0','id'=>$deal_id,'user_id'=>intval($this->uid)))->data(array('is_effect'=>'0'))->save();
            M('user')->data($data)->where(array('id'=>$this->uid))->save();
            $this->return_status('1','提交成功,进入审核阶段');
        }else{
            $deal_id = intval($_REQUEST['deal_id']);
            $count = M('deal_item')->where(array('deal_id'=>$deal_id))->order('price asc')->count();
            if($count <= 0){
                $this->error('您需要添加回报后才能进入到下一步,现在返回');
            }
            $identity_filename = "/attachment/identity/".md5($this->uid.$this->email).'.png';
            $this->assign('identity_filename',$identity_filename);
            $this->assign('deal_id',$deal_id);
            $this->display();
        }
    }

    public function upload_image(){
        $this->require_login();
        if($this->isPOST()){
            $date_str = date('Ym');
            $path = WEB_PATH.'attachment/'.$date_str.'/';
            $filename = md5(uniqid(rand())).'.png';
            $web_path = '/attachment/'.$date_str.'/'.$filename;
            $info = webuploader('name',$path,$web_path,$filename);
            $source_file = $path . $filename;
            import('ORG.Util.Image');
            Image::thumb($source_file, $source_file, '', 611, 162);
            exit($info['info']);
        }
    }

    public function upload_identity(){
        $this->require_login();
        if($this->isPOST()){
            $identity_status = M('user')->where(array('id'=>$this->uid))->getField('identity_status');
            if($identity_status == '1'){
                $status  = array('status'=>0,'info'=>'{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "您已经上传过身份证了,不能重新上传"}, "id" : "id"}');
                exit(json_encode($status,JSON_HEX_TAG));
            }
            $path = WEB_PATH.'attachment/identity/';
            $filename = md5($this->uid.$this->email).'.png';
            $web_path = '/attachment/identity/'.$filename;
            $info = webuploader('name',$path,$web_path,$filename);
            M('user')->where(array('id'=>$this->uid))->data(array('identity_status'=>'1'))->save();
            exit($info['info']);
        }
    }

	
}
?>